package sunwul.hospital.hosp.controller.api;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sunwul.hospital.hosp.service.ScheduleService;
import sunwul.hospital.hosp.util.CheckUtil;
import sunwul.hospital.common.helper.HttpRequestHelper;
import sunwul.hospital.common.result.Result;
import sunwul.hospital.model.model.hosp.Schedule;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author sunwul
 * @date 2023/11/5 14:54:25
 * @description  对接service-manage模块
 */
@Api(tags = "排班信息管理")
@RestController
@RequestMapping("/api/hosp")
public class ScheduleApiController {

    @Autowired
    ScheduleService scheduleService;

    @Autowired
    CheckUtil checkUtil;

    @ApiOperation(value = "分页获取排班信息")
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request){
        // 获取参数
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        checkUtil.checkSignKey(paramMap);
        String hoscode = (String) paramMap.get("hoscode");
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page")); // 当前页
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String) paramMap.get("limit")); // 每页大小

        Page<Schedule> schedulePage = new Page<>(page, limit);
        QueryWrapper<Schedule> wrapper = new QueryWrapper<>();
        wrapper.eq("hoscode", hoscode);
        IPage<Schedule> pageRes = scheduleService.page(schedulePage, wrapper);
        // 匹配接口返回结果
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("totalElements",pageRes.getTotal());
        resMap.put("pageNum",pageRes.getCurrent());
        resMap.put("content",pageRes.getRecords());
        return Result.ok(resMap);
    }

    @ApiOperation(value = "上传排班信息")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        // 获取参数
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        // 校验
        checkUtil.checkSignKey(paramMap);
        // 根据service-manage调用情况, manage上传为循环调用一个一个上传信息
        Schedule schedule = JSON.parseObject(JSON.toJSONString(paramMap),Schedule.class);
        // 保存或更新
        UpdateWrapper<Schedule> wrapper = new UpdateWrapper<>();
        wrapper.eq("hoscode", schedule.getHoscode());
        wrapper.eq("depcode", schedule.getDepcode());
        wrapper.eq("hos_schedule_id", schedule.getHosScheduleId());
        boolean flag = scheduleService.saveOrUpdate(schedule,wrapper);
        if (!flag) {
            return Result.fail();
        }
        return Result.ok();
    }

    @ApiOperation(value = "删除排班信息")
    @PostMapping("schedule/remove")
    public Result removeDepartment(HttpServletRequest request) {
        // 获取参数
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        // 校验
        checkUtil.checkSignKey(paramMap);
        String hosScheduleId = (String) paramMap.get("hosScheduleId");
        String hoscode = (String) paramMap.get("hoscode");
        QueryWrapper<Schedule> wrapper = new QueryWrapper<>();
        wrapper.eq("hos_schedule_id",hosScheduleId);
        wrapper.eq("hoscode",hoscode);
        boolean flag = scheduleService.remove(wrapper);
        if (!flag){
            Result.fail();
        }
        return Result.ok();
    }
}
